package com.baiye.template.dao;

import com.baiye.template.pojo.Permission;
import com.baiye.template.pojo.User;
import org.apache.ibatis.annotations.*;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author GUOZHIPENG
 * @version 1.0
 * @date 2021/8/26 16:20
 */
@Repository
public interface UserDao {

    @Insert("insert into user(username,password,email,creat_time) values(#{username},#{password},#{email},#{creatTime})")
    int register(User u);

    @Select("select * from userr where username = #{username} and password = #{password}")
    User login(User u);

    @Update("<script>" +
            "update user" +
            "<trim prefix=\"set\" suffixOverrides=\",\">"+
            "<if test='username!=null'>username=#{username},</if>" +
            "<if test='password!=null'>password=#{password},</if>" +
            "<if test='email!=null'>email=#{email},</if>" +
            "<if test='img!=null'>img=#{img},</if>" +
            "phone=#{phone}," +
            "</trim>"+
            "where id=#{id}" +
            "</script>")
    int updateUserInfo(User user);

    @Select("select * from user where username = #{username}")
    User loadUserByUsername(String username);


    @Select({"<script>" +
            "select * from user " +
            " <where> " +
//            "<if test='start!=null and size !=null'>id >=( select id from role order by id limit #{start},1 )</if>"+
            " <if test='user.id != null'>AND id=#{user.id}</if> " +
            " <if test='user.username != null'> AND username=#{user.username}</if> " +
            " </where> " +
            "</script>"})
    List<User> getUser(@Param("user") User user, @Param("start") Integer start, @Param("pageSize") Integer pageSize);
}
